package com.theexceptions.todo.view;

import java.util.Calendar;
import java.util.GregorianCalendar;

import com.theexceptions.todo.controller.TasksController;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * An activity that manages user login. Corresponds to the view: login.xml.
 * 
 * @author TheExceptions
 */
public class TaskFilterActivity extends Activity {

	private Button pickDate;
	private int dispYear;
	private int dispMonth;
	private int dispDay;

	static final int DATE_DIALOG_ID = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.tasksfilter);

		// Add a listener to the pickDate button
		pickDate = (Button) findViewById(R.id.pickDate);
		pickDate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		// Get the current date
		final Calendar c = Calendar.getInstance();
		dispYear = c.get(Calendar.YEAR);
		dispMonth = c.get(Calendar.MONTH);
		dispDay = c.get(Calendar.DAY_OF_MONTH);
		updateDisplay();

		// "completed" Spinner
		Spinner completedSpinner = (Spinner) findViewById(R.id.completedSpinner);
		ArrayAdapter<CharSequence> completedAdapter = ArrayAdapter.createFromResource(
				this, R.array.completed_array,
				android.R.layout.simple_spinner_item);
		completedAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		completedSpinner.setAdapter(completedAdapter);

		completedSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

		// "category" Spinner
		Spinner viewSpinner = (Spinner) findViewById(R.id.categorySpinner);
		ArrayAdapter<CharSequence> categoryAdapter = ArrayAdapter.createFromResource(
				this, R.array.category_array2,
				android.R.layout.simple_spinner_item);
		categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		viewSpinner.setAdapter(categoryAdapter);

		viewSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
	}

	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		}

		public void onNothingSelected(AdapterView parent) {
		}
	}

	/**
	 * Finishes the Activity when the "back" button is pressed.
	 * 
	 * @param view
	 */
	public void backButtonClicked(View view) {
		finish();
	}

	/**
	 * Transitions to the addtask view when the "add task" button is clicked.
	 * 
	 * @param view
	 */
	public void addTaskButtonClicked(View view) {
		Intent intent = new Intent(view.getContext(), AddTaskActivity.class);
		startActivityForResult(intent, 0);
	}

	/**
	 * Clears the filters for the user's tasks and transitions to the task list
	 * when the "all tasks" button is clicked.
	 * 
	 * @param view
	 */
	public void viewAllTasksClicked(View view) {
		Intent intent = new Intent(view.getContext(), TasksActivity.class);
		TasksController.clearFilters();
		startActivityForResult(intent, 0);
	}

	/**
	 * Creates filters for the user's tasks and transitions to the task list
	 * when the "go" button is clicked.
	 * 
	 * @param view
	 */
	public void goButtonClicked(View view) {
		Intent intent = new Intent(view.getContext(), TasksActivity.class);
		applyTaskFilters();
		startActivityForResult(intent, 0);
	}

	/**
	 * Updates the date in the TextView
	 */
	private void updateDisplay() {
		pickDate.setText(
				new StringBuilder()
						// Month is 0 based, add 1
						.append(dispMonth + 1).append("-")
						.append(dispDay).append("-")
						.append(dispYear).append(" ")
				);
	}

	// the callback received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener mDateSetListener =
			new DatePickerDialog.OnDateSetListener() {

				public void onDateSet(DatePicker view, int year,
						int monthOfYear, int dayOfMonth) {
					dispYear = year;
					dispMonth = monthOfYear;
					dispDay = dayOfMonth;
					updateDisplay();
				}
			};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this,
					mDateSetListener,
					dispYear, dispMonth, dispDay);
		}
		return null;
	}

	/**
	 * Takes in the raw information from the view and creates filters to sort
	 * the user's tasks.
	 */
	private void applyTaskFilters() {
		TasksController.clearFilters();

		// Add a category filter
		Spinner categorySpinner = (Spinner) this.findViewById(R.id.categorySpinner);
		String category = categorySpinner.getSelectedItem().toString();
		TasksController.addCategoryFilter(category);

		// Add a completed filter
		Spinner completedSpinner = (Spinner) this.findViewById(R.id.completedSpinner);
		String completed = completedSpinner.getSelectedItem().toString();
		TasksController.addCompletedFilter(completed);

		// Add a date filter
		GregorianCalendar date = new GregorianCalendar(dispYear, dispMonth + 1, dispDay);
		TasksController.addDateFilter(date.getTimeInMillis());
	}

}
